package com.code.train.controller;

import com.code.train.req.MemberLoginReq;
import com.code.train.req.MemberRegisterReq;
import com.code.train.req.MemberSendCodeReq;
import com.code.train.resp.CommonResp;
import com.code.train.resp.MemberLoginResp;
import com.code.train.service.MemberService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * TestController
 *
 * @param
 * @author 南汉卿
 * @return
 * @date 2025/8/2 18:02
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    MemberService memberService;

    @GetMapping("/hello")
    public CommonResp<Long> hello(){
        return CommonResp.success(memberService.count());
    }

    /**
     * 注册
     * @param registerReq
     * @return
     */
    @PostMapping("/register")
    public CommonResp<Long> register(@Valid @RequestBody MemberRegisterReq registerReq){
        return CommonResp.success(memberService.register(registerReq));
    }

    /**
     * 发送短信验证码
     * @param sendCodeReq
     * @return
     */
    @PostMapping("/send-code")
    public CommonResp<String> senCode(@Valid @RequestBody MemberSendCodeReq sendCodeReq){
        memberService.sendCode(sendCodeReq);
        return CommonResp.success();
    }

    @GetMapping("/send-picture-code")
    public CommonResp<String> sendPictureCode(@Valid MemberSendCodeReq sendCodeReq, HttpServletResponse response){
        memberService.sendPictureCode(sendCodeReq,response);
        return CommonResp.success();
    }

    /**
     * 登录
     * @param memberLoginReq
     * @return
     */
    @PostMapping("/login")
    public CommonResp<MemberLoginResp> login(@Valid @RequestBody MemberLoginReq memberLoginReq){
        return CommonResp.success(memberService.login(memberLoginReq));
    }
}
